package com.appspot.showup_app.showup;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.appspot.showup_app.showup.model.ShowUpAppointmentMessage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class AgendaActivity extends ActionBarActivity {

    private static final String LOG_TAG = "Showup";
    ListView lstAgenda;

    public SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agenda);

        if (!ApplicationData.getInstance().isLoggedIn())
        {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }

        lstAgenda = (ListView) findViewById(R.id.lstAgenda);
        DatabaseHelper mDbHelper = new DatabaseHelper(this);
        db = mDbHelper.getWritableDatabase();

        List<ShowUpAppointmentMessage> apts = DatabaseHelper.getAppointments(db);
        AgendaAdapter adapter = new AgendaAdapter(this, apts);
        lstAgenda.setAdapter(adapter);


        //set scrolling to start from closest appointment
        Date currentTime=new Date();
        int position;
        for(position=0; position<apts.size(); position++) {
            ShowUpAppointmentMessage apt = apts.get(position);
            Date startTime = DateTimeUtil.fromTimestamp(apt.getStartTime());
            if (startTime.after(currentTime))
                break;
        }

        final int currentPosition=position;
        lstAgenda.clearFocus();
        lstAgenda.post(new Runnable() {

            @Override
            public void run() {

                lstAgenda.setSelection(currentPosition);
            }
        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_agenda, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.menu_action_add) {
            startActivity(new Intent(this, AddEditAppointmentActivity.class));
        }
        else if (id == R.id.menu_action_customers) {
            startActivity(new Intent(this, CustomersActivity.class));
        }
        else {
            startActivity(new Intent(this, EditBusinessInfoActivity.class));
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        List<ShowUpAppointmentMessage> apts = DatabaseHelper.getAppointments(db);
        AgendaAdapter adapter = new AgendaAdapter(this, apts);
        lstAgenda.setAdapter(adapter);

        //set scrolling to start from closest appointment
        Date currentTime=new Date();
        int position;
        for(position=0; position<apts.size(); position++) {
            ShowUpAppointmentMessage apt = apts.get(position);
            Date startTime = DateTimeUtil.fromTimestamp(apt.getStartTime());
            if (startTime.after(currentTime))
                break;
        }

        final int currentPosition=position;
        lstAgenda.clearFocus();
        lstAgenda.post(new Runnable() {

            @Override
            public void run() {

                lstAgenda.setSelection(currentPosition);
            }
        });
    }
}
